﻿
@{
    Layout = null;
}
@using System.Text;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/lib/layui/css/layui.css" rel="stylesheet" />
    <title>模块设置</title>
</head>
<body>
    <div>
        <div class="demo-tree" id="test7"></div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left:0;text-align:center;">
                <button class="layui-btn layui-btn-blue" id="formDemo">确认选择</button>
            </div>
        </div>
    </div>
    <script src="~/lib/layui/layui.js"></script>
    <script>
        layui.use(['tree', 'util'], function () {
            var tree = layui.tree
                , layer = layui.layer
                , util = layui.util
                , $ = layui.jquery;
            @{
                StringBuilder ids = new StringBuilder();
                foreach (var item in ViewBag.IdList)
                {
                    ids.Append(item + ",");
                }
               string idstr = ids.ToString().TrimEnd(',');
             }
            var RMRoleId='@ViewBag.RMRoleId';
            var ids = [@idstr];
            var data2 = "";

            $.post("/System/GetModuleAll", {}, function (res) {
                data2 = res;
                tree.render({
                    elem: '#test7'
                    , data: data2
                    , showCheckbox: true
                    , id: 'demoId'
                });
                tree.setChecked('demoId',ids);
            },"json");
            $("#formDemo").click(function () {//提交
                var checkData = tree.getChecked('demoId');//获得选中的节点
                if (checkData.length == 0) { //判断复选框是否被选中
                    layer.msg("请至少选择一个节点", { icon: 6 });
                    return false;
                } else {
                    var ids1 = [];
                    var chil = [];
                    for (var i = 0; i < checkData.length; i++) {
                        var node = checkData[i];
                        ids1.push(node.id);
                        chil = node.children;
                        for (var j = 0; j < chil.length; j++) {
                            var children = chil[j];
                            ids1.push(children.id);
                        }
                    }
                    var list = ids1.join(',');
                    $.post("/System/GUpdateModule", { RMModuleId: list, RMRoleId: RMRoleId }, function (res) {
                        if (res.status) {
                            layer.msg(res.message)
                        }
                        else {
                            layer.msg(res.message)
                        }
                    }, "Json");
                }
            });
        });
    </script>
</body>
</html>
